/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.chassis;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class GridPopupPanel extends JPanel {

	private int row = 4;
	private int col = 5;
	private int borderSize = 2;

	private int selectRow = -1;
	private int selectCol = -1;

	private JLabel label = new JLabel("cancel");
	private CellPanel cellPanel = new CellPanel();
	private GridButton button;

	public GridPopupPanel(final GridButton button) {
		this.button = button;
		this.setPreferredSize(new Dimension(160, 120));

		this.setLayout(new BorderLayout());
		this.add(cellPanel, BorderLayout.CENTER);
		this.add(label, BorderLayout.SOUTH);
		label.setPreferredSize(new Dimension(10, 20));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				button.getMenu().setVisible(false);
			}
		});
	}

	class CellPanel extends JPanel {
		public CellPanel() {
			this.addMouseMotionListener(new MouseMotionAdapter() {
				public void mouseMoved(MouseEvent e) {
					setSelectionCell(e);
				}

				public void mouseDragged(MouseEvent e) {
					setSelectionCell(e);
				}
			});
			this.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (selectRow >= 1 && selectCol >= 1) {
						button.setSelectionCell(selectRow, selectCol);
					}
				}

				public void mouseExited(MouseEvent e) {
					label.setText("cancel");
					selectRow = -1;
					selectCol = -1;
					repaint();
				}
			});
		}

		private void setSelectionCell(MouseEvent e) {
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					Rectangle2D.Double rect = getCell(i, j, false);
					if (rect.contains(e.getPoint())) {
						selectRow = i + 1;
						selectCol = j + 1;
						label.setText("row=" + selectRow + "  col=" + selectCol);
						this.repaint();
						return;
					}
					label.setText("cancel");
					selectRow = -1;
					selectCol = -1;
					this.repaint();
				}
			}
		}

		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d = (Graphics2D) g;
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					Rectangle2D.Double rect = getCell(i, j, true);
					g2d.setColor(Color.WHITE);
					if (i < selectRow && j < selectCol) {
						g2d.setColor(new Color(0, 50, 100));
					}
					g2d.fill(rect);
					g2d.setColor(Color.BLACK);
					g2d.draw(rect);
				}
			}
		}

		private Rectangle2D.Double getCell(int rowIndex, int colIndex, boolean considerBorder) {
			Dimension dimension = this.getSize();
			double w = dimension.getWidth() - borderSize;
			double h = dimension.getHeight() - borderSize;
			double cellW = w / col;
			double cellH = h / row;
			double x = cellW * colIndex;
			double y = cellH * rowIndex;
			double border = 0;
			if (considerBorder) {
				border = borderSize;
			}
			Rectangle2D.Double rect = new Rectangle2D.Double(x + border, y + border, cellW - 2 * border, cellH - 2 * border);
			return rect;
		}
	}
}